<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>优贷云 - 贷款管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/layui/css/layui.css">
    <style>
        .layui-layout-admin .layui-header {
            background: linear-gradient(120deg, #3498db, #2980b9) !important;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .layui-nav {
            background: transparent !important;
        }

        .layui-side {
            background: #fff !important;
            box-shadow: 2px 0 8px rgba(0, 0, 0, 0.05);
        }

        .layui-side .layui-nav {
            background: #fff !important;
        }

        .layui-nav-tree .layui-nav-item a {
            color: #666 !important;
        }

        .layui-nav-tree .layui-nav-child dd.layui-this,
        .layui-nav-tree .layui-nav-child dd.layui-this a,
        .layui-nav-tree .layui-this,
        .layui-nav-tree .layui-this>a {
            background: linear-gradient(120deg, #3498db, #2980b9) !important;
            color: #fff !important;
        }

        .layui-nav-tree .layui-nav-item a:hover {
            background: rgba(52, 152, 219, 0.1) !important;
        }

        .layui-logo {
            color: #fff !important;
            font-weight: 500;
            font-size: 18px;
        }

        .layui-card {
            margin: 15px;
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
        }

        .table-search-box {
            padding: 15px;
            border-radius: 4px;
            background-color: #fff;
            margin-bottom: 15px;
        }

        .layui-table-tool {
            background: #fff;
            border-radius: 4px 4px 0 0;
        }

        .layui-table-box {
            border-radius: 0 0 4px 4px;
        }

        .status-pending {
            color: #e6a23c;
        }

        .status-approved {
            color: #67c23a;
        }

        .status-rejected {
            color: #f56c6c;
        }

        .status-loaned {
            color: #409EFF;
        }

        .status-repaid {
            color: #67C23A;
        }

        .status-overdue {
            color: #F56C6C;
        }

        .status-failed {
            color: #909399;
        }

        /* 状态标签样式 */
        .status-tag {
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 12px;
        }

        .status-0 {
            background: #E6A23C20;
            color: #E6A23C;
        }

        .status-1 {
            background: #67C23A20;
            color: #67C23A;
        }

        .status-2 {
            background: #F56C6C20;
            color: #F56C6C;
        }

        .status-3 {
            background: #409EFF20;
            color: #409EFF;
        }

        .status-4 {
            background: #67C23A20;
            color: #67C23A;
        }

        .status-5 {
            background: #F56C6C20;
            color: #F56C6C;
        }

        .status-6 {
            background: #90939920;
            color: #909399;
        }
    </style>
</head>

<body class="layui-layout-body">
    <div class="layui-layout layui-layout-admin">
        <!-- 头部区域 -->
        <div class="layui-header">
            <div class="layui-logo">优贷云</div>
            <!-- 引入顶部用户信息模板 -->
            {% include "back/templates/admin/common/header-user.html" %}
        </div>

        <!-- 引入左侧导航栏模板 -->
        {% include "back/templates/admin/common/menu.html" %}

        <!-- 内容主体区域 -->
        <div class="layui-body">
            <!-- 原有的卡片内容移到这里 -->
            <div class="layui-card">
                <div class="layui-card-body">
                    <!-- 数据表格 -->
                    <table id="loanTable" lay-filter="loanTable"></table>
                </div>
            </div>
        </div>

        <!-- 底部固定区域 -->
        <div class="layui-footer">
            © 2024 优贷云管理系统
        </div>
    </div>

    <!-- 行工具栏模板 -->
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="approve">通过</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="reject">拒绝</a>
    </script>


    <!-- 放款方式模板 -->
    <script type="text/html" id="loanMethodTpl">
        {% if d.loan_method == 0 %}
            <span>支付宝</span>
        {% elif d.loan_method == 1 %}
            <span>微信</span>
        {% elif d.loan_method == 2 %}
            <span>银行卡</span>
        {% endif %}
    </script>

    <!-- 状态模板 -->
    <script type="text/html" id="statusTpl">
        <span class="status-tag status-{{d.status}}">
            {% if d.status == 0 %}
                待审核
            {% elif d.status == 1 %}
                已通过
            {% elif d.status == 2 %}
                已拒绝
            {% elif d.status == 3 %}
                已放款
            {% elif d.status == 4 %}
                已还款
            {% elif d.status == 5 %}
                已逾期
            {% elif d.status == 6 %}
                已流标
            {% endif %}
        </span>
    </script>

    <script src="/static/admin/layui/layui.js"></script>
    <script>
        layui.use(['element', 'table', 'form'], function () {
            var element = layui.element
                , table = layui.table
                , $ = layui.$
                , form = layui.form;

            // 表格实例
            table.render({
                elem: '#loanTable'
                , id: 'loanTable'
                , url: '/web/loan/apply_list/'
                , toolbar: false
                , defaultToolbar: ['filter', 'exports', 'print']
                , title: '贷款申请数据表'
                , method: 'post'
                , cols: [[
                    { field: 'id', title: 'ID', width: 60, fixed: 'left', sort: true }
                    , { field: 'loan_order_number', title: '订单编号', width: 150 }
                    , { field: 'user__username', title: '申请人', width: 100 }
                    , { field: 'amount', title: '申请金额', width: 100, sort: true }
                    , { field: 'description', title: '订单描述', width: 120 }
                    , { field: 'status', title: '状态', width: 90 }
                    , { field: 'loan_amount', title: '放款金额', width: 100 }
                    , { field: 'loan_method', title: '放款方式', width: 90, templet: '#loanMethodTpl' }
                    , { field: 'loan_time', title: '放款时间', width: 160 }
                    , { field: 'repayment_time', title: '还款时间', width: 160 }
                    , { field: 'overdue_days', title: '逾期天数', width: 90 }
                    , { field: 'overdue_amount', title: '逾期金额', width: 100 }
                    , { field: 'create_time', title: '申请时间', width: 160, sort: true }
                    , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150 }
                ]]
                , page: true
                , height: 'full-120'
                , parseData: function (res) {
                    // 处理404错误
                    if (res.code === 404) {
                        window.location.href = '/web/404/';
                        return;
                    }

                    let a = {
                        "code": res.code,
                        "msg": res.msg,
                        "count": res.count,
                        "data": res.data.LoanOrder
                    }
                    a.data.forEach(item => {
                        item.user__username = item.user_info.name
                    });
                    return a;
                }
                , response: {
                    statusCode: 0
                }
            });

            // 监听行工具事件
            table.on('tool(loanTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'approve') {
                    layer.confirm('确认通过此贷款申请？', {
                        btn: ['确定', '取消']
                    }, function (index) {
                        $.ajax({
                            url: '/api/loan/loan/',
                            type: 'POST',
                            data: {
                                loan_order_number: data.loan_order_number,
                                loan_amount: data.amount,
                                loan_method: data.loan_method,
                            },
                            success: function (res) {
                                if (res.code === 404) {
                                    window.location.href = '/web/404/';
                                    return;
                                }
                                if (res.code === 200) {
                                    layer.msg('审核通过成功');
                                    table.reload("loanTable");
                                } else {
                                    layer.msg(res.msg || '操作失败');
                                }
                            },
                            error: function () {
                                window.location.href = '/web/404/';
                            }
                        });
                        layer.close(index);
                    });
                } else if (obj.event === 'reject') {
                    layer.confirm('确认拒绝此贷款申请？', {
                        btn: ['确定', '取消']
                    }, function (index) {
                        $.ajax({
                            url: '/api/loan/reject/',
                            type: 'POST',
                            data: {
                                loan_order_number: data.loan_order_number,
                            },
                            success: function (res) {
                                if (res.code === 200) {
                                    layer.msg('已拒绝申请');
                                    table.reload('loanTable');
                                } else {
                                    layer.msg(res.msg || '操作失败');
                                }
                            }
                        });
                        layer.close(index);
                    });
                }
            });

            // 搜索表单提交
            form.on('submit(searchForm)', function (data) {
                table.reload('loanTable', {
                    where: data.field
                });
                return false;
            });
        });
    </script>
</body>

</html>